import { Toast } from 'antd-mobile'

export const createAction = type => (payload = {}) => ({ type, payload })

export const createNamedAction = namespace => type => (payload = {}) => ({ type: `${namespace}/${type}`, payload })

const toastDuration = 3000
export const errorToast = error => {
  if (!error) {
    Toast.fail('未知错误', toastDuration)
  } else if (typeof error === 'string') {
    Toast.fail(error, toastDuration)
  } else if (typeof error.message === 'string') {
    Toast.fail(error.message, toastDuration)
  } else {
    Toast.fail(`${error}`, toastDuration)
  }
}

let shadowIndexCount = 0
export const uniIndex = () => {
  shadowIndexCount += 1
  return shadowIndexCount
}
